package com.itheima.reggie.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.dto.resp.R;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/statics")
public class StaticsController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    /**
     * 接口一：菜系营收占比分析
     * 请求url地址：/statics/dishCash
     * 请求方式：GET
     * 请求参数：无
     *
     * @return
     */
    @GetMapping("/dishCash")
    public R getDish() {
        //获取category
        List<Category> categories = categoryService.list();

        List<JSONObject> list = categories.stream().map(category -> {
            //获取分类名称
            String name = category.getName();
            BigDecimal decimal = null;
            if (name.endsWith("菜")) {
                //获取分类的id
                Long id = category.getId();
                LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(Dish::getCategoryId, id);
                //根据分类id获取菜品
                List<Dish> dishList = dishService.list(wrapper);
                decimal = new BigDecimal(0);
                //获取菜品对应的总价
                for (Dish dish : dishList) {
                    decimal = decimal.add(dish.getPrice());
                }
            }
            //创建json对象接收返回给前端
            JSONObject itm = new JSONObject();
            itm.put("name", name);
            itm.put("value", decimal);
            return itm;

        }).collect(Collectors.toList());

        return R.success(list);


    }

    /**
     * 接口二：套餐营收占比分析
     * 请求url地址：/statics/dishCash
     * 请求方式：GET
     * 请求参数：无
     * 响应结果【格式统一】：
     *
     * @return
     */
    @GetMapping("/setmealCash")
    public R getSetmeal() {
        //获取category
        List<Category> categories = categoryService.list();

        List<JSONObject> list = categories.stream().map(category -> {
            //获取分类名称
            String name = category.getName();
            BigDecimal decimal = null;
            if (name.endsWith("套餐")) {
                //获取分类的id
                Long id = category.getId();
                LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(Setmeal::getCategoryId, id);
                //根据分类id获取菜品
                List<Setmeal> setmealList = setmealService.list(wrapper);
                decimal = new BigDecimal(0);
                //获取菜品对应的总价
                for (Setmeal setmeal : setmealList) {
                    decimal = decimal.add(setmeal.getPrice());
                }
            }
            //创建json对象接收返回给前端
            JSONObject itm = new JSONObject();
            itm.put("name", name);
            itm.put("value", decimal);
            return itm;

        }).collect(Collectors.toList());

        return R.success(list);
    }

}
